/*
 * ScraperApp.java
 */

package scraper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import scraper.dao.Data;

/**
 * The main class of the application.
 */
public class ScraperApp extends SingleFrameApplication {

    private File configurationFile;

    public File getConfigurationFile() {
        return configurationFile;
    }

    public void setConfigurationFile(File configurationFile) {
        this.configurationFile = configurationFile;
    }
    
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new ScraperView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    public List<Data> scrape() throws ScraperException
    {
        Data data=null;
        List<Data> dataObjs = new ArrayList<Data>();
        try{

            ResourceConfiguration rc = new ResourceConfiguration(configurationFile);
            List<WebResource> webResources = rc.getAllWebResources();
            for (WebResource webResource : webResources)
            {
                for (DataLocator locator : webResource.getDataLocators())
                {
                    if(webResource.getName().contains("theunderminejournal")){
                        data = Data.load(locator);
                        dataObjs.add(data);                        
                    }

                }
            }                  
        }catch(Exception e){
            throw new ScraperException("error during Data Load: "+e.getMessage(), e);
        }     
        return dataObjs;
    }
    /**
     * A convenient static getter for the application instance.
     * @return the instance of ScraperApp
     */
    public static ScraperApp getApplication() {
        return Application.getInstance(ScraperApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(ScraperApp.class, args);
    }
}
